package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Past;
import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO dto
     * @return R
     */
    @ApiOperation("分类分页查询")
    @GetMapping("/page")
    public Result<PageBean> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageBean pageBean = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageBean);
    }

    /**
     * 新增分类
     *
     * @param categoryDTO 请求参数
     * @return R
     */
    @ApiOperation("新增分类")
    @PostMapping
    public Result<String> add(@RequestBody CategoryDTO categoryDTO) {
        categoryService.add(categoryDTO);
        return Result.success();
    }

    /**
     * 根据id删除分类
     *
     * @param id 分类id
     * @return R
     */
    @ApiOperation("根据id删除分类")
    @DeleteMapping
    public Result deleteById(Long id) {
        categoryService.deleteById(id);
        return Result.success();
    }

    /**
     * 修改分类
     *
     * @param categoryDTO dto
     * @return R
     */
    @ApiOperation("修改分类")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     * 启用、禁用分类
     *
     * @param status 状态
     * @param id     分类id
     * @return R
     */
    @ApiOperation("启用、禁用分类")
    @PostMapping("status/{status}")
    public Result updateStatus(@PathVariable Integer status, Long id) {
        categoryService.updateStatus(status, id);
        return Result.success();
    }


    /**
     * 根据类型查询分类
     *
     * @param type 分类类型：1为菜品分类，2为套餐分类
     * @return R
     */
    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    public Result selectByType(Integer type) {
        log.info("根据类型查询分类:{}", type);
        List<Category> categories = categoryService.selectByType(type);
        return Result.success(categories);
    }

}
